<?php
namespace App\Models\System;

use App\Exceptions\ApiException;
use App\Providers\LengthAwarePaginatorProvider;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
* 角色和部门关联表 模型
* @create 2023-08-22 22:53:45
*/
class SysRoleDeptModel extends \App\Models\BaseModel {
    use HasFactory;

    const Table         = "sys_role_dept";
    const PrimaryKey    = "role_id";
    public $timestamps  = false;

    static $Wheres = ['role_id','dept_id'];     // 直接定义column, 默认where(column, '=', value)
    public function __construct($attributes = []) {
        parent::__construct($attributes);

        // static::$Wheres['role_id']   = fn($q, $v) => $q->where(atn('role_id'), '=', $v);
    }


    /** xxxx */
    // public function user() {
        // return $this->hasOne(User::class, 'id', 'uid');
    // }

    /** ========== 常用查询 ========== */
    static $Vo  = ['*', ];
    static $Voi = ['*', ];

        /**
     * 通过角色ID删除角色和部门关联
     * 
     * @param $roleId 角色ID
     * @return 结果
     */
    public function deleteRoleDeptByRoleId(int $roleId): int {
        return $this->deleteRoleDept([$roleId]);
    }

    /**
     * 批量删除角色部门关联信息
     * 
     * @param $ids 需要删除的数据ID
     * @return int 结果
     */
    public function deleteRoleDept(array $ids): int {
        return $this
            ->whereIn('role_id', $ids)
            ->delete()
            ;
    }

    /**
     * 查询部门使用数量
     * 
     * @param $deptId 部门ID
     * @return 结果
     */
    public function selectCountRoleDeptByDeptId(int $deptId): int {
        return $this
            ->select(static::PrimaryKey)
            ->where('dept_id', '=', $deptId)
            ->count()
            ;
    }

    /**
     * 批量新增角色部门信息
     * 
     * @param $roleDeptList 角色部门列表
     * @return int 结果
     */
    public function batchRoleDept(array $roleDeptList): int {
        foreach($roleDeptList as $it) {
            $values[] = ['role_id'=>$it['roleId'], 'dept_id'=>$it['deptId']];
        }
        return DB::table(static::Table)
            ->insert($values)
            ;
    }

    /** 根据RoleId获取 */
    public function selectByRoleId($role_id, $dept_id=null): SysRoleDeptModel {
        $where = [['role_id', '=', "{$role_id}"]];
        if ($dept_id) $where[] = ['dept_id', '=', "{$dept_id}"];
        return throw_str(
            static::row($where) ?? "数据($role_id)为空~"
        );
    }
    /** 根据DeptId获取 */
    public function selectByDeptId($dept_id): SysRoleDeptModel {
        $where = [['dept_id', '=', "{$dept_id}"]];
        return throw_str(
            static::row($where) ?? "数据($dept_id)为空~"
        );
    }


    /** 获取列表 @return SysRoleDept[]+paginator() */
    public function selectList(int|array $pag=1, array $param=[]): LengthAwarePaginatorProvider {
        return static::rows_pag($pag, [], fn($q)=>$q
                    // ->when(isset($param['role_id']), fn($q)=>$q->where('role_id', '=', $param['role_id']))
                    ->wheres($param, 'role_id','dept_id')
                    ->orderByDesc("role_id")
        );
    }


    /** 插入数据 */
    public function insertData(array $data): SysRoleDeptModel {
        return throw_str(
            static::row_add([
                // 'role_id'            => $data['role_id'],
                // 'dept_id'            => $data['dept_id'],
            ])
        );
    }


    /** 更新数据 */
    public function updateData(int|array $idOrWhere, array $data): SysRoleDeptModel {
        $values = static::when(
                    $data, 
                    // 'role_id',
                    // 'dept_id',
                );
        return throw_str(
            static::row_update($idOrWhere, $values)
        );
    }


    /** 删除数据 */
    public function deleteData(int|array $idOrWhere): SysRoleDeptModel {
        return throw_str(
            static::row_delete($idOrWhere)
        );
    }



}